
package capa_logica;

import java.io.StringReader;
import javax.json.Json;
import javax.json.JsonException;
import javax.json.JsonObject;
import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;

// Esta clase se encarga de decodificar para el trasporte en WebSocket
public class WSTransporteDecodificador implements Decoder.Text<WSTransporte> 
{
    @Override
    public WSTransporte decode(String string) throws DecodeException 
    {
        JsonObject jsonObject = Json.createReader(new StringReader(string)).readObject();
        return  new WSTransporte(jsonObject);
    }

    @Override
    public boolean willDecode(String string) 
    {
         try {
            Json.createReader(new StringReader(string)).readObject();
            return true;
             } 
         catch (JsonException ex) 
         {
            ex.printStackTrace();
            return false;
         }
    }

    @Override
    public void init(EndpointConfig config) 
    {
        System.out.println("init");
    }

    @Override
    public void destroy() 
    {
        System.out.println("destroy");
    }
}